// packageCommunity/pages/releaseVideo/index.js
import{comments} from "../../../js/modules/mall.js" 
import { 
  api,util
} from '../../../js/util'; 
import {
  upload
} from '../../../js/modules/material.js'
import {
  account
} from "../../../js/modules/gloab.js";
var app = getApp();
const pageBehavior = require('../../../js/common/PageBehavior')

Page({
  behaviors: [pageBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    uploadVideoList:'',//添加视频
    uploadShopList:[],//添加商品列表
    shopImageList:[],
    readerValue:'',
    currentNum:0,
    type:1,//帖子类型
    SectionId:'',//板块id
    SectionName:'',//板块Name
    VideoList:[],//视频
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })
  },
  /**
   * 添加视频
   */
  bindUploadEvent:function(){
    var that = this;
    var list = [];
    var uploadlist = [];
    wx.chooseVideo({
      sourceType: ['album','camera'],
      camera: 'back',
      success(res) {
        if(res.size >= 20971520){
          util.alert('上传视频不能大于20M!');
          return;
        }
        var tempFilePaths=[];
        console.log(res);
        tempFilePaths.push(res.tempFilePath)
        console.log(tempFilePaths,'tempFilePaths');
        // tempFilePaths.map(function (item) {
        //   that.data.uploadVideoList.push(item);
        // })
        that.setData({
          uploadVideoList:res.tempFilePath
        })
      //   console.log(tempFilePaths,'-----res');
      //   wx.showToast({
      //     title: '正在上传...',
      //     icon: 'loading',
      //     mask: true,
      //     duration: 10000
      //   });
      //   console.log(app.dns.proprietorId,'-----');
      //   account.get_upload_token(app.dns.proprietorId,(upload_token)=>{
      //     console.log(tempFilePaths,'-------tep');
      //     wx.uploadFile({
      //       url: api.gwDomain+"/upload/upload",
      //       filePath: tempFilePaths[0],
      //       name: 'uploadfile_ant'+2,
      //       header:{
      //         token:upload_token
      //       },
      //       success: function (res) {
      //         if (res.statusCode == 200) {
      //           let content=JSON.parse(res.data);
      //           console.log(res.data);
      //           if(content.success){
      //             console.log(uploadlist,'------1');
      //           }else{

      //           }
      //         }else{}
      //       },
      //       fail: function (e) {

      //       },
      //       complete: function (e) {
      //         //上传完毕，作一下提示
      //         wx.showToast({
      //           title: '上传成功',
      //           icon: 'success',
      //           duration: 2000
      //         });
      //         console.log(uploadlist,'-------imgs');
      //       }
      //     })
      //  });
        var data = {num:2001,type:2,tempFilePaths:tempFilePaths};
        upload.upload_image_server(data, function (image_list) {
          if (image_list.length==0){
            util.alert("上传失败");
            return;
          }
          image_list.map(function (item) {
            that.data.VideoList.push(item);
          });
        })
        // console.log(that.data.VideoList,'------');
      },
      fail(err){
        console.log(err);
      }
    })
  },
  /**
   * 删除
   */
  onDelVideoEvent:function(e){
    this.setData({
      uploadVideoList:''
    })
  },
  /**
  * 添加商品
  */
  bindUploadShop:function(e){
    var goods = e.currentTarget.dataset.goods;
    wx.navigateTo({
      url: '/packageCommunity/pages/addGoods/index?good='+JSON.stringify(goods),
    })
  },
  /**
   * 删除--添加商品
   */
  onDelShopEvent:function(e){
    var that = this;
    var imgidx = e.currentTarget.dataset.imgidx;
    var image_list = that.data.uploadShopList;
    image_list.splice(imgidx, 1);
    var virtual_list = that.data.shopImageList;
    virtual_list.splice(imgidx, 1);
    that.setData({
      uploadShopList: image_list,
      shopImageList: virtual_list
    })
  },

  /**
   * 文本域 
   */
  changeReaderValue(e){
    let value = e.detail.value;
    this.setData({
      readerValue:value,
      currentNum:value.length
    })
  },
  /**
   * 发布
   */
  btnRelease:function(){
    var that = this;
    if(!this.data.uploadVideoList){
      util.alert('请添加视频');
      return;
    }else if(!this.data.readerValue){
      util.alert('请输入内容');
      return;
    }else if(this.data.uploadShopList.length == 0){
      util.alert('请添加商品');
      return;
    }else if(!this.data.SectionName){ 
      util.alert('请选择板块');
      return; 
    }
    let data = {
      Type:this.data.type,
      Title:'',
      Image:[],
      Video:JSON.stringify(this.data.VideoList),
      Content:this.data.readerValue,
      LinkSrc:'',LinkName:'',
      SectionId:this.data.SectionId,
      RelateType:'',SaleType:'',SaleValue:'',ScopeType:'',ScopeValue:'',
      goods:this.data.uploadShopList
    };
    console.log(this.data.VideoList,'---video');
    return;
    comments.getBindPost(data,function(content){
      // console.log(content,'---发布');
      if(content.Success){
        wx.redirectTo({
          url: '/packageCommunity/pages/success/index',
        })
      }else{
        util.alert(content.Message);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.SectionId) {
      this.setData({
        SectionId: this.data.SectionId,
        SectionName:this.data.SectionName
      });
    }
    if(currPage.data.uploadShopList.length>0){
      this.setData({
        uploadShopList:this.data.uploadShopList
      })
    }
    console.log(this.data.uploadShopList,'-----list');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})